package com.hspedu.tankgame06;

import java.io.*;
import java.util.Vector;

//退出游戏，保存游戏数据
public class Record {
    //定义变量，记录被击毁坦克数量
    private static int allEnemyTankNum = 0;
    //定义IO对象
    private static BufferedWriter bw = null;//存
    private static BufferedReader br = null;//读
    //存盘文件地址
    private static String recordFile = "src\\myRecord.txt";

    //获取MyPanel中的敌人坦克Vector
    private static Vector<EnemyTank> enemyTanks = null;

    //将node存入
    private static Vector<Node> nodes = new Vector<>();

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Record.enemyTanks = enemyTanks;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Record.allEnemyTankNum = allEnemyTankNum;
    }

    public static String getRecordFile() {
        return recordFile;
    }

    //读取文件，恢复相关信息，重置回上一局游戏
    public static Vector<Node> getNodes() {
        try {
            br = new BufferedReader(new FileReader(recordFile));
            allEnemyTankNum = Integer.parseInt(br.readLine());
            //读取剩下的坦克信息，生成Node集合
            String line = "";//206 120 1
            while ((line = br.readLine()) != null) {
                String[] s = line.split(" ");
                int x = Integer.parseInt(s[0]);
                int y = Integer.parseInt(s[1]);
                int direct = Integer.parseInt(s[2]);
                Node node = new Node(x, y, direct);
                nodes.add(node);
            }
        } catch (IOException e) {//将异常放大
            e.printStackTrace();
        } finally {
            //关闭文件流
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return nodes;
    }

    //当游戏退出时，将allEnemyTank存到recordFile中
    //增加功能：继续上一局游戏 -> 记录敌方坦克坐标和方向
    public static void KeepRecord() {
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(allEnemyTankNum + "\r\n");
            //遍历Vector
            for (int i = 0; i < enemyTanks.size(); i++) {
                //取出敌人坦克
                EnemyTank enemyTank = enemyTanks.get(i);
                //判断存活
                if (enemyTank.isALive) {
                    //存入敌人坦克的 坐标 + 方向
                    String record = enemyTank.getX() + " " +
                            enemyTank.getY() + " " + enemyTank.getDirect();
                    //写入到文件中
                    bw.write(record + "\r\n");
                    //关闭文件流
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static int getAllEnemyTank() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTank(int allEnemyTank) {
        Record.allEnemyTankNum = allEnemyTank;
    }

    //当我方坦克击毁一个敌方坦克
    public static void addAllEnemyTankNum() {
        Record.allEnemyTankNum++;
    }
}
